﻿using Koala.Pro.CodeGenerator.UI.Common;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Data;

namespace Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema
{
    public class DbInstanceInfo
    {
        private IDictionary<string, DatabaseInfo> databaseList = new Dictionary<string, DatabaseInfo>();

        private string connectString;

        private IListData databaseAssembly;

        private string databaseType;

        private string noSuffixOfTableName;

        public IDictionary<string, DatabaseInfo> DatabaseList
        {
            get
            {
                return databaseList;
            }
            set
            {
                databaseList = value;
            }
        }

        public string ConnectString
        {
            get
            {
                return connectString;
            }
            set
            {
                connectString = value;
            }
        }

        public IListData DatabaseAssembly
        {
            get
            {
                return databaseAssembly;
            }
            set
            {
                databaseAssembly = value;
            }
        }

        public string DatabaseType
        {
            get
            {
                return databaseType;
            }
            set
            {
                databaseType = value;
            }
        }

        public IDictionary<string, DatabaseInfo> ListDatabases()
        {
            databaseAssembly = DatabaseFactory.Create(DatabaseType);
            if (databaseAssembly != null)
            {
                if (noSuffixOfTableName != null)
                {
                    databaseAssembly.NoSuffixOfTableName = noSuffixOfTableName;
                }
                DatabaseList = databaseAssembly.ListDatabases();
                foreach (KeyValuePair<string, DatabaseInfo> database in DatabaseList)
                {
                    database.Value.db = this;
                }
            }
            return DatabaseList;
        }

        public DbInstanceInfo(string databaseType)
        {
            this.databaseType = databaseType;
        }

        public DbInstanceInfo(string databaseType, string noSuffixOfTableName)
        {
            this.databaseType = databaseType;
            this.noSuffixOfTableName = noSuffixOfTableName;
        }

        public override string ToString()
        {
            return StringUtil.ToString(this);
        }
    }

}
